package com.znf4.controller.user;

import java.util.ArrayList;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.znf4.entity.ApiCommonResultVo;
import com.znf4.id.IdGenerator;
import com.znf4.user.order.entity.UserOrder;
import com.znf4.user.order.server.UserOrderServer;

/**
 * 订单
 */
@RestController
@RequestMapping("/gzh/user/order")
public class UserOrderController {
	
	private static final Logger LOG = LoggerFactory.getLogger(UserOrderController.class);
	
	@Autowired
	private UserOrderServer userOrderServer ;
	
	@Autowired
	private IdGenerator idGenerator ;
	
	

	/**
	 * 函数功能说明 ： 添加订单
	 *
	 * @参数： @return
	 * @return String
	 * @throws
	 */
	@RequestMapping(value = "/add", method = { RequestMethod.POST, RequestMethod.GET })
	public ApiCommonResultVo add(UserOrder userOrder) {
		
		try {
			userOrder.setId(idGenerator.getPaymentOrderId());
			userOrderServer.addUserOrder(userOrder);
		    return new ApiCommonResultVo(0, "操作成功", "" ); 
		} catch (Exception e) {
			LOG.info("添加订单接口异常 ：" + e.getMessage());
			return new ApiCommonResultVo(-2, "系统异常", "");
		}
		
		
	}

	
	
	/**
	 * 函数功能说明 ： 修改订单
	 *
	 * @参数： @return
	 * @return String
	 * @throws
	 */
	@RequestMapping(value = "/update", method = { RequestMethod.POST, RequestMethod.GET })
	public ApiCommonResultVo update(UserOrder userOrder) {
		
		try {
			userOrderServer.updateUserOrder(userOrder);
		    return new ApiCommonResultVo(0, "操作成功", "" ); 
		} catch (Exception e) {
			LOG.info("修改订单接口异常 ：" + e.getMessage());
			return new ApiCommonResultVo(-2, "系统异常", "");
		}
		
	}

	/**
	 * 函数功能说明 ： 删除用户订单
	 *
	 * @参数： @return
	 * @return String
	 * @throws
	 */
	@RequestMapping(value = "/delOne", method = { RequestMethod.POST, RequestMethod.GET })
	public ApiCommonResultVo delOne(UserOrder userOrder) {
		
		try {
			userOrderServer.delOneUserOrder(userOrder); 
		    return new ApiCommonResultVo(0, "操作成功", "" ); 
		} catch (Exception e) {
			LOG.info("修改订单接口异常 ：" + e.getMessage());
			return new ApiCommonResultVo(-2, "系统异常", "");
		}
		
	}

	/**
	 * 函数功能说明 ： 删除多条订单
	 * @参数： @return
	 * @return String
	 * @throws
	 */
	@RequestMapping(value = "/delAll", method = { RequestMethod.POST, RequestMethod.GET })
	public ApiCommonResultVo delAll(String[] ids) {
		
		try {
			List<UserOrder> list = new ArrayList<>() ;
			for(int i=0 ; i < ids.length ; i ++ ){
				UserOrder userOrder = new UserOrder() ;
				userOrder.setId(ids[i]);
				list.add(userOrder) ;
			}
			userOrderServer.delAllUserOrder(list);
		    return new ApiCommonResultVo(0, "操作成功", "" ); 
		} catch (Exception e) {
			LOG.info("删除多条订单接口异常 ：" + e.getMessage());
			return new ApiCommonResultVo(-2, "系统异常", "");
		}
		
		
	}
	

	/**
	 * 函数功能说明 ： 获取多个订单
	 *
	 * @参数： @return
	 * @return String
	 * @throws
	 */
	@RequestMapping(value = "/queryAll", method = { RequestMethod.POST, RequestMethod.GET })
	public ApiCommonResultVo queryAll(UserOrder userOrder  , Model model) {
		
		try {
			List<UserOrder> list = userOrderServer.queryAll(userOrder);
		    return new ApiCommonResultVo(0, "操作成功", list); 
		} catch (Exception e) {
			LOG.info("获取多个订单接口异常 ：" + e.getMessage());
			return new ApiCommonResultVo(-2, "系统异常", "");
		}
		
	}
	
	

}
